<?php

namespace controllers;

class HomeApi extends Api {
    protected        $verifyType = 2;
    protected static $pageSize   = 15;

    public function __construct() {
        parent::__construct();
        self::$post = json_decode(file_get_contents('php://input'), true);
    }

    /**
     * 返回一个json数据
     *
     * @param        $list
     * @param        $count
     * @param array  $data
     * @param string $keyName
     *
     * @return array
     */
    protected static function returnJson($list, $count = 0, $data = [], $keyName = '') {
        if (!$keyName) {
            $keyName = isset($list[0]) && !is_string($list) ? 'list' : 'find';
        }
        $json = [
            $keyName => $list,
        ];
        if ($count) {
            $json['countPage'] = ceil($count / static::$pageSize);
        }
        if ($data) {
            $json = array_merge($json, $data);
        }

        return success($json, 200);
    }

    /**
     * 错误信息
     *
     * @param $msg
     *
     * @return string
     */
    protected static function returnError($msg) {
        return error($msg, 404);
    }
}
